﻿/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Tools_UploadX : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs args)
	{

		if (!this.IsPostBack)
		{
			//  reserve a spot in Session for the UploadInfo object
			this.Session["UploadInfo"] = new UploadInfo { IsReady = false };

			if (Request.QueryString["s"] != null && Request.QueryString["s"] != "")
			{
				this.Session["UploadInfo"] = new UploadInfo { Path = Request.QueryString["s"] };
			}
		}
	}

	[System.Web.Services.WebMethod]
	[System.Web.Script.Services.ScriptMethod]
	public static object GetUploadStatus()
	{
		//  get the length of the file on disk and divide that
		//  by the length of the stream ...
		UploadInfo info = HttpContext.Current.Session["UploadInfo"] as UploadInfo;

		if (info != null && info.IsReady)
		{
			int soFar = info.UploadedLength;
			int total = info.ContentLength;
			
			int percentComplete = (int)Math.Ceiling((double)soFar / (double)total * 100);
			string message = string.Format(Resources.DatabaseStrings.UploadingX + " '{0}' ... {1} of {2} Bytes", info.FileName, soFar, total);
			
			//  return the percentage
			return new { percentComplete = percentComplete, message = message };
		}

		//  not ready yet ...
		return null;
	}
}
